<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- // 面包屑区域 -->
      <el-card v-if="!!this.$route.query.subjectID">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{ name }}</el-breadcrumb-item>
          <el-breadcrumb-item>目录管理</el-breadcrumb-item>
        </el-breadcrumb>
      </el-card>
      <el-card shadow="never">
        <!-- // 导航栏区域 -->
        <div class="box">
          <!-- // 左侧边栏区域 -->
          <div class="left">
            <el-form :inline="true" :model="form" class="demo-form-inline">
              <el-form-item label="目录名称" prop="directoryName">
                <el-input v-model="form.directoryName" size="small "></el-input>
              </el-form-item>
              <el-form-item label="状态">
                <el-select v-model="form.state" placeholder="请选择">
                  <el-option label="启用" value="0"></el-option>
                  <el-option label="禁用" value="1"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button @click="remove">清除</el-button>
                <el-button type="primary" @click="reach">搜索</el-button>
              </el-form-item>
            </el-form>
          </div>
          <!-- // 新增按钮 -->
          <div class="right">
            <el-button v-if="!!this.$route.query.subjectID" @click="back" type="text">+返回学科</el-button>
            <el-button class="addbtn" type="success" @click="openAdd" size="small ">git push -u origin master
              <i class="el-icon-edit"></i>新增目录</el-button
            >
          </div>
        </div>
        <div class="msg">
          <el-alert type="info"><i class="el-icon-info"></i> 共有{{ totals }}条记录</el-alert>
        </div>
        <!-- // 表格区域 -->
        <el-table :data="list" stripe style="width: 100%">
          <el-table-column type="index" label="序号" width="130"> </el-table-column>
          <el-table-column prop="subjectName" label="所属学科" width="140"> </el-table-column>
          <el-table-column prop="directoryName" label="目录名称" width="140"> </el-table-column>
          <el-table-column prop="username" label="创建者" width="140"> </el-table-column>
          <el-table-column prop="addDate" label="创建日期" width="160">
            <template slot-scope="scope">
              {{ scope.row.addDate | parseTimeByString }}
            </template>
          </el-table-column>
          <el-table-column prop="totals" label="面试题数量" width="140"> </el-table-column>
          <el-table-column prop="state" label="状态" width="140">
            <template slot-scope="scope">
              <span v-if="!scope.row.state">已启用</span>
              <span v-else-if="scope.row.state">已禁用</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="changestate(scope.row)" v-if="scope.row.state == 0">禁用 </el-button>
              <el-button type="text" @click="changestate(scope.row)" v-else-if="scope.row.state == 1">启用</el-button>
              <el-button type="text" :disabled="!scope.row.state" @click="editData(scope.row)">修改</el-button>
              <el-button type="text" :disabled="!scope.row.state || !!scope.row.totals" @click="delData(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="form.page"
          :page-sizes="pagesizes"
          :page-size="form.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totals"
        >
        </el-pagination>
      </el-card>
      <!-- // 新增面板 -->
      <addCard :addShow.sync="addShow" @updateDate="getList" ref="myson"></addCard>
    </div>
  </div>
</template>

<script>
import { list as getCompanys } from '@/api/hmmm/companys'
import { list as getQuestion } from '@/api/hmmm/questions'
import { list as getListApi, changeState as changeStateApi, detail as delDataApi } from '@/api/hmmm/directorys.js'
export default {
  components: {
    addCard: () => import('@/module-hmmm/components/directorys-add.vue')
  },
  data () {
    return {
      form: {
        directoryName: '',
        state: null,
        // 选中的页容量
        pagesize: 10,
        // 当前选中的页码
        page: 1,
        subjectID: ''
      },
      // 原本的数据
      list: [],
      // 页容量集合
      pagesizes: [5, 10, 15, 20, 50],
      // 总计
      totals: 0,
      addShow: false,
      name: ''
    }
  },

  methods: {
    // 点击返回上一页面
    back () {
      this.$router.push('/subjects/list')
    },
    // 点击搜索, 进行筛选
    async reach () {
      this.form.page = 1
      this.getList()
    },
    // 点击修改数据,并且从子组件中调用方法进行数据回填
    async editData (id) {
      this.addShow = true
      // 进行数据回填
      this.$refs.myson.getDetail(id)
    },
    // 点击删除,删除数据
    delData (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(async () => {
          await delDataApi(id)
          this.$message.success('删除成功')
          this.getList()
        })
        .catch(() => {
          this.$message({
            message: '已取消删除'
          })
        })
    },
    // 点击新增按钮 打开面板
    // 获取简单列表数据,为新增作准备
    async openAdd () {
      this.addShow = true
    },
    // 当前的id 和当前的对应的状态
    // 点击改变启用和禁用按钮的状态
    async changestate (row) {
      await changeStateApi({
        id: row.id,
        state: row.state === 0 ? 1 : 0
      })
      if (!this.state) {
        row.state = !row.state
      }
      this.$message.success('操作成功')
    },
    // 清空关键字输入框
    remove () {
      this.form = {
        directoryName: '',
        state: ''
      }
    },
    // 获取列表数据
    async getList () {
      const { data: res } = await getListApi({
        // 获取当前页容量
        size: this.form.pagesize,
        page: this.form.page,
        directoryName: this.form.directoryName,
        state: this.form.state,
        // 获取学科id从路由参数中
        subjectID: this.$route.query.subjectID
      })
      this.list = res.items
      this.totals = res.counts
      this.name = this.$route.query.name
    },
    handleSizeChange (size) {
      this.form.pagesize = size
      this.getList()
    },
    handleCurrentChange (page) {
      this.form.page = page
      this.getList()
    }
  },
  created () {
    getCompanys()
    getQuestion()
    this.getList()
  }
}
</script>

<style lang="scss" scoped>
.dashboard-container {
  .box {
    position: relative;
    .left {
      flex: 1;
    }
    .right {
      position: absolute;
      top: 0;
      right: 0;
      .el-breadcrumb {
        display: block;
      }
      .el-icon-edit {
        padding: 0 5px;
      }
    }
  }
  .el-pagination {
    text-align: right;
    margin: 10px 0;
  }
}
</style>
